import { getLayerMap } from '@/service';

export const initGlobalEffectLayer = ({ light, process, background, environment }: any) => {
  const { cimInstance } = window;
  // 光源
  cimInstance.api.updateClock(light?.illumination);
  cimInstance.api.updateLight(light?.lightSource);
  // 后期
  process?.forEach((item: any) => {
    cimInstance.api.updatePostProcessByName(item.key, item.value);
  });
  // 背景图
  if (background.backgroundType === 'bkgImg') {
    cimInstance.api.updateBackgroundImage({
      showImage: true,
      dataSourceUrl: background.backgroundImage.url,
    });
  }
  // 天空盒
  if (background.backgroundType === 'SkyBox') {
    if (background?.skybox?.param) {
      const param = toRaw(background?.skybox.param);
      const skybox = {
        enable: true,
        index: 1,
        name: 'skyName',
        type: 'skybox',
        dayPart: param,
      };
      cimInstance.api.changeSkyBox(skybox);
    }
  }
  const enableNormal = environment.normal.filter((x: any) => x.params.enable);
  const enableOther = environment.other.filter((x: any) => x.params.enable);
  enableNormal.forEach((item: any) => {
    cimInstance.api.updateWeather({ ...item.params, type: item.key });
  });
  enableOther.forEach((item: any) => {
    if (item.key === 'sky') {
      cimInstance.api.setSkyVisible(item.params.enable);
    } else if (item.key === 'cloudLayer') {
      const layerMap = getLayerMap();
      if (layerMap.get(item.params.code)) {
        cimInstance.api.setLayerVisible(item.params.layerName, item.params.enable);
      } else {
        cimInstance.api.addRectangleLayer({
          enable: true,
          type: 'rectangle',
          name: '云层',
          datas: [[-180, -90, 180, 90]],
          orbitTheEarth: true,
          images: [require('@/assets/png/earth_cloud.png')],
        });
        layerMap.set(item.params.code, item.params.code);
      }
    }
  });
};
